package wsepForum.ui.swing.innerPanels;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.MatteBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import wsepForum.common.elements.Message;
import wsepForum.common.elements.SearchResult;
import wsepForum.network.common.GUIAnswer;
import wsepForum.network.common.GUIAnswer.AnswerType;
import wsepForum.network.common.GUIRequest.RequestType;
import wsepForum.ui.swing.outerPanels.AOuterPanel;
import wsepForum.ui.swing.workers.ForumWorker;
import javax.swing.JScrollPane;

public class SearchResultPanel extends AInnerPanel {

	private static final long serialVersionUID = 8780235564341084937L;
	private Vector<SearchResult> searchResVector;
	private AInnerPanel backPanel;
	private Message message;

	public SearchResultPanel(Vector<SearchResult> searchRes, AInnerPanel backtoPanel, AOuterPanel oPanel) {
		super(oPanel);
		this.searchResVector = searchRes;
		this.backPanel = backtoPanel;
		setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		setLayout(null);
		setBackground(new Color(230, 230, 250));
		setBounds(20, 93, 716, 364);

		JLabel lblSearchResult = new JLabel("Search Results");
		lblSearchResult.setFont(new Font("Cooper Black", Font.PLAIN, 18));
		lblSearchResult.setHorizontalAlignment(SwingConstants.CENTER);
		lblSearchResult.setBounds(277, 11, 162, 36);
		add(lblSearchResult);

		Vector<String> messageHeadlines = new Vector<String>();
		for (SearchResult srcRes : this.searchResVector) {
			messageHeadlines.add("("+(int)(srcRes.getScore()*100)+"%) "+srcRes.getMessage().getHeadLine());
		}

		JLabel lblMessges = new JLabel("Messges:");
		lblMessges.setFont(new Font("Cooper Black", Font.PLAIN, 16));
		lblMessges.setBounds(10, 61, 136, 26);
		add(lblMessges);
		JButton btnBack = new JButton("Back");
		btnBack.setBounds(615, 11, 91, 23);
		btnBack.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ForumWorker<AInnerPanel> worker = new ForumWorker<AInnerPanel>(outerPanel.getConnection()){
					@Override
					protected AInnerPanel doInBackground() throws Exception {
						return backPanel;
					}
					@Override
					public void done(){
						try {
							outerPanel.swapInnerPanel(get());
						} catch (InterruptedException e) {
							JOptionPane.showMessageDialog(null, "system is unavalible at the moment, please try again later\n" + e.getMessage(), "Error",JOptionPane.ERROR_MESSAGE);
						} catch (ExecutionException e) {
							JOptionPane.showMessageDialog(null, "system is unavalible at the moment, please try again later\n" + e.getMessage(), "Error",JOptionPane.ERROR_MESSAGE);
						}
					}
				};
				worker.execute();
			}
		});
		add(btnBack);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 92, 311, 261);
		add(scrollPane);
		final JList messageList = new JList(messageHeadlines);
		scrollPane.setViewportView(messageList);

		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(360, 92, 346, 261);
		add(scrollPane_1);


				final JTextArea messageTextArea = new JTextArea();
				scrollPane_1.setViewportView(messageTextArea);
		messageList.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				ForumWorker<AInnerPanel> worker = new ForumWorker<AInnerPanel>(outerPanel.getConnection()){
					@Override
					protected AInnerPanel doInBackground() throws Exception {
						int selectedMsg = messageList.getSelectedIndex();
						int askedMsgID = searchResVector.elementAt(selectedMsg).getMessage().getElementID();
						GUIAnswer ans = request(RequestType.GET_MESSAGE, new Object[] {askedMsgID}) ;
						outerPanel.setNumOfConnected(ans.getNumOfConnected());
						if(ans.getAnswerType().equals(AnswerType.ANSWER)){
							message = (Message)ans.getArguments()[0];
						}
						return null;
					}
					@Override
					public void done(){
						messageTextArea.setText("Message by: " + (message.getUserName() != null ? message.getUserName() : "Guest") +
								"\nDate: " + message.getTimeStamp() +
								"\nHeadline: " + message.getHeadLine()+
								"\n--------------------------------------\n" + message.getBody());
					}
				};
				worker.execute();
			}
		});
	}
}
